package qe.spider.grab;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.text.UnicodeUtil;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import qe.spider.vo.StockList;
import qe.spider.vo.TimeSharing;

import java.io.IOException;
import java.util.Date;


//看盘
@Slf4j
public class GrabGeGuFenShi {
    private String userID;
    private String token;

    public GrabGeGuFenShi(String userID, String token) {
        this.userID = userID;
        this.token = token;
    }

    public static void main(String[] args) throws IOException {
//        new GrabGeGuFenShi("111775r", "13f1764f728c8db11f85b98e524bec3c").grab("603976");
        String json = FileUtil.readString("FenShi.json", "UTF-8");
        TimeSharing ts = new Gson().fromJson(json, TimeSharing.class);
        log.debug(ts.toString());
    }


    public StockList grab(String stockCode) throws IOException {
        FormBody formBody = new FormBody.Builder()
                .add("c", "PCArrangeData")
                .add("a", "GetHQPlate")
                .add("StockID", stockCode)
                .add("SelType", "1, 2, 3, 8, 9, 5, 6, 7")
                .build();

        OkHttpClient client = new OkHttpClient();
        final Request request = new Request.Builder()
                .url("https://pchq.kaipanla.com/w1/api/index.php")
                .header("Accept", "application/json, text/javascript, */*; q=0.01")
//                .header("Accept-Encoding", "br")
                .header("Accept-Language", "zh-CN,zh;q=0.9")
                .header("Cache-Control", "no-cache")
                .header("Connection", "keep-alive")
                .header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
                .header("Host", "pchq.kaipanla.com")
                .header("Origin", "https://www.kaipanla.com")
                .header("Pragma", "no-cache")
                .header("Sec-Fetch-Dest", "empty")
                .header("Sec-Fetch-Mode", "cors")
                .header("Sec-Fetch-Site", "same-site")
                .header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36 Edg/109.0.1518.140")
                .header("sec-ch-ua", "\"Google Chrome\";v=\"119\", \"Chromium\";v=\"119\", \"Not?A_Brand\";v=\"24\"")
                .header("sec-ch-ua-mobile", "?0")
                .header("sec-ch-ua-platform", "\"Windows\"")
                .header("Referer", "https://www.kaipanla.com/index.php/stock/index?id=" + stockCode)
                .header("Content-Length", "88")
                .post(formBody)
                .build();
        Gson gson = new Gson();
        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);


            String result = UnicodeUtil.toString(response.body().string());
            log.info(result);
            StockList list = gson.fromJson(result, StockList.class);
            list.grabTime();
            log.debug(list.toString());
            return list;
        }

    }


}
